﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using Cvk.FAQs;

namespace MakeupVenus.Manager
{
    public partial class ManFAQ : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                BindDropDownList(ref ddlcategory);
                ddlcategory.Items.Insert(0, new ListItem("-- All --", "0"));
                LoadData();
            }
        }
        private void LoadData()
        {
            List<Cvk_FQA> list = null;
            try
            {

                if (ddlcategory.SelectedValue == "0")
                {
                    var o = from p in FAQBll.claFAQs.GetAllList(Utility.strConnect)
                            where p.Question.Contains(txtSearch.Text.Trim()) || p.Answer.Contains(txtSearch.Text.Trim())
                            select p;
                    list = o.ToList();
                }
                else
                {
                    var o = from p in FAQBll.claFAQs.GetFQAByCategoryId(Convert.ToInt32(ddlcategory.SelectedValue), Utility.strConnect)
                            where p.Question.Contains(txtSearch.Text.Trim()) || p.Answer.Contains(txtSearch.Text.Trim())
                            select p; 
                    list = o.ToList();
                }
            
            }
            catch
            { }
            grv.DataSource = list;
            grv.DataBind();
        }
        private void BindDropDownList(ref DropDownList ddl)
        {
            try
            {
                List<Cvk_FQACategory> list = FAQBll.claFAQCategory.GetAllList(Utility.strConnect, true);

                ddl.DataSource = list;
                ddl.DataTextField = "Name";
                ddl.DataValueField = "FQACategoryId";
                ddl.DataBind();
            }
            catch { }
        }
        protected void lnkbtDelete_Click(object sender, EventArgs e)
        {
            try
            {
                LinkButton btn = (LinkButton)sender;
                int id = Convert.ToInt32(btn.CommandArgument);
                FAQBll.claFAQs.Delete(id, Utility.strConnect);
                LoadData();
            }
            catch { }
        }
        protected void grv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Cvk_FQA info = (Cvk_FQA)e.Row.DataItem;
                LinkButton lnkbtDelete = (LinkButton)e.Row.FindControl("lnkbtDelete");
                lnkbtDelete.OnClientClick = "return confirm('Bạn có chắc không?')";
                lnkbtDelete.CommandArgument = info.FQAId.ToString();
                LinkButton btnActive = (LinkButton)e.Row.FindControl("btnActive");
                btnActive.CommandArgument = info.FQAId.ToString();
                LinkButton btnUnActive = (LinkButton)e.Row.FindControl("btnUnActive");
                btnUnActive.CommandArgument = info.FQAId.ToString();
                Label lblCreateDate = (Label)e.Row.FindControl("lblCreateDate");
                lblCreateDate.Text = info.CreateDate.Value.ToString("dd/MM/yyyy hh:mm");
                Label lblAnswer = (Label)e.Row.FindControl("lblAnswer");
                HyperLink hplName = (HyperLink)e.Row.FindControl("hplName");
                hplName.NavigateUrl = "FAQReply.aspx?faqid=" + info.FQAId.ToString();
                LinkButton btnFre = (LinkButton)e.Row.FindControl("btnFre");
                btnFre.CommandArgument = info.FQAId.ToString();
                LinkButton btnUnFre = (LinkButton)e.Row.FindControl("btnUnFre");
                btnUnFre.CommandArgument = info.FQAId.ToString();

                if (info.Answer != null && info.Answer.Trim() != "")
                {
                    lblAnswer.Text = "trả lời";
                    lblAnswer.ForeColor = System.Drawing.Color.BlueViolet;
                }
                else
                {
                    lblAnswer.Text = "Chưa trả lời";
                    lblAnswer.ForeColor = System.Drawing.Color.Red;
                }

                if (info.IsActive == false)
                {
                    btnUnActive.Visible = true;
                    btnActive.Visible = false;
                }
                else
                {
                    btnUnActive.Visible = false;
                    btnActive.Visible = true;
                }
                if (info.IsFrequency == false)
                {
                    btnFre.Visible = false;
                    btnUnFre.Visible = true;
                }
                else
                {
                    btnFre.Visible = true;
                    btnUnFre.Visible = false;
                }
            }
        }
        protected void grv_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grv.PageIndex = e.NewPageIndex;
            LoadData();
        }
        protected void btnActive_Click(object sender, EventArgs e)
        {
            try
            {
                LinkButton btn = (LinkButton)sender;
                int id = Convert.ToInt32(btn.CommandArgument);
                FAQBll.claFAQs.UpdateIsActive(id, false, Utility.strConnect);
                LoadData();
            }
            catch { }
        }
        protected void btnUnActive_Click(object sender, EventArgs e)
        {
            try
            {
                LinkButton btn = (LinkButton)sender;
                int id = Convert.ToInt32(btn.CommandArgument);
                FAQBll.claFAQs.UpdateIsActive(id, true, Utility.strConnect);
                LoadData();
            }
            catch { }
        }
        protected void btnUnFre_Click(object sender, EventArgs e)
        {
            try
            {
                LinkButton btn = (LinkButton)sender;
                int id = Convert.ToInt32(btn.CommandArgument);
                FAQBll.claFAQs.UpdateIsFrequency(id, false, Utility.strConnect);
                LoadData();
            }
            catch { }
        }
        protected void btnFre_Click(object sender, EventArgs e)
        {
            try
            {
                LinkButton btn = (LinkButton)sender;
                int id = Convert.ToInt32(btn.CommandArgument);
                FAQBll.claFAQs.UpdateIsFrequency(id, true, Utility.strConnect);
                LoadData();
            }
            catch { }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                LoadData();
            }
            catch { }
        }

        protected void ddlcategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}